﻿using System;

namespace Rules
{
    public class MemberOfEnumRule<T> : RuleBase<T>
    {
        public MemberOfEnumRule(string propertyName)
            : base(propertyName)
        {
            ValidateGenericTypeIsAnEnumeration();
        }

        public MemberOfEnumRule(string propertyName, string description)
            : base(propertyName, description)
        {
            ValidateGenericTypeIsAnEnumeration();
        }

        private void ValidateGenericTypeIsAnEnumeration()
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("T must be an enumeration", "T");
            }
        }

        protected override bool DoEvaluate(T subject)
        {
            return Enum.IsDefined(typeof(T), subject);
        }

        protected override string GetDefaultDescription()
        {
            return string.Format("{0} must be an item in the list", PropertyName);
        }
    }
}
